package cn.campus.service.article.impl;

import cn.campus.dao.article.CommentMapper;
import cn.campus.enums.article.ResultEnum;
import cn.campus.exception.article.ArticleException;
import cn.campus.pojo.article.dto.CommentDTO;
import cn.campus.pojo.article.dto.PageInfo;
import cn.campus.pojo.article.po.Comment;
import cn.campus.service.UserService;
import cn.campus.service.article.ArticleService;
import cn.campus.service.article.CommentService;
import cn.campus.util.KeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论服务接口实现类
 * Created by linyanbin on 2018/9/20.
 */
@Service
@Slf4j
public class CommentServiceImpl implements CommentService{

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;

    @Override
    public CommentDTO findOne(String commentId) {
        // 查询评论
        CommentDTO commentDTO = commentMapper.unionFindCommentById(commentId);
        if (commentDTO == null) {
            log.error("【查询评论】评论不存在，commentId={}", commentId);
            throw new ArticleException(ResultEnum.COMMENT_NOT_EXIST);
        }
        return commentDTO;
    }

    @Override
    public PageInfo<CommentDTO> findAllByArticleId(String articleId, Integer page, Integer size) {
        List<CommentDTO> commentDTOList = commentMapper.unionFindListByArticleId(articleId, (page-1)*size, size);
        long count = commentMapper.countByArticleId(articleId); // 统计总记录数
        PageInfo<CommentDTO> pageInfo = new PageInfo<>(commentDTOList, page, size);
        pageInfo.setCount(count);
        pageInfo.setTotalPage((int)Math.ceil(count*1.0/size));
        return pageInfo;
    }

    @Override
    public List<CommentDTO> findListByArticleId(String articleId) {
        List<Comment> commentList = commentMapper.findListByArticleId(articleId, null, null);
        return commentList.stream().map((comment) -> {
            CommentDTO commentDTO = new CommentDTO();
            BeanUtils.copyProperties(comment, commentDTO);
            return commentDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public CommentDTO save(CommentDTO commentDTO) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        if (comment.getCommentId() != null) {
            int effectedNum = commentMapper.updateComment(comment);
            if (effectedNum <= 0) {
                log.error("【更新评论】更新失败：commentId = {}", comment.getCommentId());
                throw new ArticleException(ResultEnum.UPDATE_COMMENT_FAIL);
            }
            return commentDTO;
        } else {
            comment.setCommentId(KeyUtils.getUUID());
            int effectedNum = commentMapper.insertComment(comment);
            if (effectedNum <= 0) {
                log.error("【添加评论】添加失败：comment = {}", comment);
                throw new ArticleException(ResultEnum.ADD_COMMENT_FAIL);
            }
            commentDTO.setCommentId(comment.getCommentId());
            return commentDTO;
        }
    }

    @Override
    public void deleteById(String commentId) {
        int effectedNum = commentMapper.deleteCommentById(commentId);
        if (effectedNum <= 0) {
            log.error("【删除评论】删除失败：commentId = {}", commentId);
            throw new ArticleException(ResultEnum.REMOVE_COMMENT_FAIL);
        }
    }

}
